//
//  GovCenterFloorViewController.m
//  homeland
//
//  Created by SunShine on 14-1-18.
//  Copyright (c) 2014年 xzysun. All rights reserved.
//

#import "GovCenterFloorViewController.h"
#import "IconButton.h"
#import "EnterpriseWorkEntity.h"
#import "ListViewController.h"

#define ICON_MARGINS 10.0
#define ICON_SPACES 15.0
#define ICON_WIDTH 90.0
#define ICON_HEIGHT 110.0

@interface GovCenterFloorViewController ()

@property (strong, nonatomic) NSDictionary *config;
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@end

@implementation GovCenterFloorViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"政务服务中心";
    self.navigationItem.rightBarButtonItem = rightNavBtnWithImgName(@"网上预约", self, @selector(appointmentButtonAction:));
    NSString *path = [[NSBundle mainBundle] pathForResource:@"floor" ofType:@"plist"];
    self.config = [NSDictionary dictionaryWithContentsOfFile:path];
    [self drawView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Buttons
-(void)appointmentButtonAction:(id)sender
{
    [self showAlertWithTitle:@"提示" Msg:@"正在建设中"];
}

#pragma mark - Draw Method
-(void)drawView
{
    CGFloat totalHeight = 0.0;
    NSArray *floors = [self.config objectForKey:@"floors"];
    for (int i = 0, count = floors.count; i < count; i++) {
        NSDictionary *floor = [floors objectAtIndex:i];
        totalHeight = [self drawFloorWithDic:floor Index:i Offset:totalHeight];
    }
    //控制scroll内容的高度
    self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, totalHeight);
}

-(CGFloat)drawFloorWithDic:(NSDictionary *)dic Index:(NSInteger)index Offset:(CGFloat)offset
{
    NSString *name = [dic objectForKey:@"name"];
    NSLog(@"准备绘制楼层:%@", name);
    //楼层标题
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, offset, SCREEN_WIDTH, 30.0)];
    titleLabel.text = name;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor darkGrayColor];
    titleLabel.backgroundColor = [UIColor lightGrayColor];
    offset += 30.0;
    [self.scrollView addSubview:titleLabel];
    //楼层
    NSArray *items = [dic objectForKey:@"items"];
    for (int i = 0, count = items.count; i < count; i++) {
        NSDictionary *itemDic = [items objectAtIndex:i];
        NSString *itemName = [itemDic objectForKey:@"name"];
        NSString *itemImage = [itemDic objectForKey:@"image"];
        NSString *itemKey = [itemDic objectForKey:@"key"];
        NSLog(@"准备绘制图标:%@,对应的图片为:%@", itemName, itemImage);
        IconButton *button = [[[NSBundle mainBundle]loadNibNamed:@"IconButton" owner:self options:nil] objectAtIndex:0];
        CGFloat iconX = ICON_MARGINS + i%3*(ICON_WIDTH+ICON_SPACES);
        CGFloat iconY = offset + ICON_MARGINS + i/3*(ICON_HEIGHT+ICON_SPACES);
        CGRect iconRect = CGRectMake(iconX, iconY, ICON_WIDTH, ICON_HEIGHT);
        button.frame = iconRect;
        button.iconImageView.image = [UIImage imageNamed:itemImage];
        button.iconTitleLabel.text = itemName;
        button.targetStr = itemKey;
        button.tag = index * 1000 + i;
        //点击事件
        [button addTarget:self action:@selector(tapIconEvent:) forControlEvents:UIControlEventTouchUpInside];
        [button addTarget:self action:@selector(iconTouchedDown:) forControlEvents:UIControlEventTouchDown];
        [button addTarget:self action:@selector(iconTouchCancel:) forControlEvents:UIControlEventTouchCancel];
        [button addTarget:self action:@selector(iconTouchCancel:) forControlEvents:UIControlEventTouchDragExit];
        [self.scrollView addSubview:button];
    }
    //计算高度
    int numOfLines = ceil([items count]/3.0);
    CGFloat totalHeight = ICON_MARGINS*2+numOfLines*ICON_HEIGHT+(numOfLines-1)*ICON_SPACES;
    offset += totalHeight;
    return offset;
}

#pragma mark - Touch Methods
//icon的点击事件
-(void)tapIconEvent:(id)sender
{
    IconButton *icon = (IconButton *)sender;
    NSLog(@"click icon:%d", icon.tag);
    icon.backgroundColor = [UIColor clearColor];
    //进入下一级页面
    EnterpriseWorkEntity *entity = [EnterpriseWorkEntity new];
    entity.businessName = icon.targetStr;
    NSDictionary *config = [NSDictionary dictionaryWithObjectsAndKeys:@"政务服务中心指南列表", @"key", entity, @"dataItem", nil];
    ListViewController *listVC = [[ListViewController alloc] initWithNibName:@"ListViewController" bundle:nil];
    listVC.config = config;
    [self.navigationController pushViewController:listVC animated:YES];
}

//icon的按下事件，这里主要是变个颜色
-(void)iconTouchedDown:(id) sender
{
    IconButton *icon = (IconButton *)sender;
    icon.backgroundColor = [UIColor colorWithRed:0.50 green:0.68 blue:0.84 alpha:1.0];
    NSLog(@"icon %d touch down", icon.tag);
}

//icon的取消事件，包含拖拽出控件，这里主要是变回颜色
-(void)iconTouchCancel:(id)sender
{
    IconButton *icon = (IconButton *)sender;
    icon.backgroundColor = [UIColor clearColor];
    NSLog(@"icon %d touch cancel", icon.tag);
}

@end
